﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.FullTextSearch
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LuceneIndexingEditor : Form, IComponentEditor
    {
        /// <summary>
        /// 
        /// </summary>
        public LuceneIndexingEditor()
        {
            InitializeComponent();
        }

        private void LuceneIndexingEditor_Load(object sender, EventArgs e)
        {

        }

        #region Implementation of IComponentEditor

        private object _ActivityNode;
        private LuceneIndexingComponent _IndexingComponent;

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get
            {
                return _ActivityNode;
            }
            set
            {
                _ActivityNode = value;
            }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get
            {
                return typeof (LuceneIndexingComponent);
            }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._IndexingComponent = (LuceneIndexingComponent) wfComponent;

            LuceneIndexingInstruction instruction = (LuceneIndexingInstruction) this._IndexingComponent.Instruction;
            this.txt_ConnStr.Text = instruction.ConnStr;
            this.rt_SQL.Text = instruction.SqlStatement;
            this.PopulateFields(instruction.ConnStr,instruction.SqlStatement);
            this.SelectFieldInComboBox(this.cbo_KeyField,instruction.KeyField);
            this.dv_Fields.Rows.Clear();
            if(instruction.ContentFields !=null && instruction.ContentFields.Count>0)
            {
                foreach(string contentField in instruction.ContentFields)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellFieldName=new DataGridViewTextBoxCell();
                    cellFieldName.Value = contentField;
                    dr.Cells.Add(cellFieldName);
                    DataGridViewTextBoxCell cellFieldWeight=new DataGridViewTextBoxCell();
                    cellFieldWeight.Value = 1;
                    dr.Cells.Add(cellFieldWeight);
                    this.dv_Fields.Rows.Add(dr);
                }
            }
            this.txt_IndexOutputFilePath.Text = instruction.IndexingOutputFilePath;
        }

        private void PopulateFields(string connStr, string sql)
        {
            this.cbo_KeyField.Items.Clear();
            this.cbo_TextFields.Items.Clear();
            TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, sql);
            if(cols !=null && cols.Length>0)
            {
                foreach(TableColumn col in cols)
                {
                    this.cbo_KeyField.Items.Add(col.ColumnName);
                    this.cbo_TextFields.Items.Add(col.ColumnName);
                }
            }
        }

        private void SelectFieldInComboBox(ComboBox comboBox, string field)
        {
            if(!string.IsNullOrEmpty(field) && comboBox.Items.Count>0)
            {
                for(int i=0;i<comboBox.Items.Count;i++)
                {
                    if(comboBox.Items[i].ToString()==field)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            LuceneIndexingInstruction instruction = (LuceneIndexingInstruction) this._IndexingComponent.Instruction;
            instruction.ConnStr = this.txt_ConnStr.Text;
            instruction.SqlStatement = this.rt_SQL.Text;
            instruction.KeyField = this.cbo_KeyField.Text;
            instruction.ContentFields=new List<string>();
            foreach(DataGridViewRow dr in this.dv_Fields.Rows)
            {
                if(!dr.IsNewRow && dr.Cells[0].Value !=null)
                {
                    string fieldName = dr.Cells[0].Value.ToString();
                    instruction.ContentFields.Add(fieldName);
                }
            }
            instruction.IndexingOutputFilePath = this.txt_IndexOutputFilePath.Text;
            this._IndexingComponent.Instruction = instruction;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void btn_RefreshFields_Click(object sender, EventArgs e)
        {
            this.PopulateFields(this.txt_ConnStr.Text,this.rt_SQL.Text);
        }

        private void btn_AddField_Click(object sender, EventArgs e)
        {
            if(this.cbo_TextFields.SelectedIndex>=0)
            {
                string fieldName = this.cbo_TextFields.SelectedItem.ToString();
                double weight =(double) this.num_Weights.Value;
                DataGridViewRow dr = new DataGridViewRow();
                DataGridViewTextBoxCell cellField=new DataGridViewTextBoxCell();
                cellField.Value = fieldName;
                dr.Cells.Add(cellField);
                DataGridViewTextBoxCell cellWeight=new DataGridViewTextBoxCell();
                cellWeight.Value = weight;
                dr.Cells.Add(cellWeight);
                this.dv_Fields.Rows.Add(dr);
            }
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
                this.txt_IndexOutputFilePath.Text = dlg.SelectedPath;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._IndexingComponent);
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
